package com.taocore.top.title.action;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.taobao.api.ApiException;
import com.taobao.api.domain.Item;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.response.ItemGetResponse;
import com.taobao.api.response.ItemcatsGetResponse;
import com.taobao.api.response.ProductGetResponse;
import com.taocore.top.title.service.TaobaoService;

@Results({
	  @Result(location="optimize-form.jsp")
})
public class OptimizingAction extends ActionBase{

	static Logger _log = Logger.getLogger(OptimizingAction.class.getName());
	
	private long numIid;
	private Item item;
	private String[] leafCategoryWords;
	private String product;
	
	public String execute()
	{
		String session = getSessionId();
		String fields = "num_iid,title,pic_url,cid,props_name,product_id";
		try {
			ItemGetResponse itemGetRsp = TaobaoService.getItem(numIid, fields, session);
			if (itemGetRsp.isSuccess())
			{
				item = itemGetRsp.getItem();
				ItemcatsGetResponse catsRsp = TaobaoService.getCategory(item.getCid());
				if (catsRsp.isSuccess())
				{
					List<ItemCat> cats = catsRsp.getItemCats();
					ItemCat cat = cats.get(0);
					setLeafCategoryWords(cat.getName().split("/"));
				}
				else
				{
					_log.warning(TaobaoService.getError(catsRsp));
				}
				if (item.getProductId() != null)
				{
					ProductGetResponse productRsp = TaobaoService.getProduct(item.getProductId());
					if (productRsp.isSuccess())
					{
						product = productRsp.getProduct().getName();
					}
					else
					{
						_log.warning(TaobaoService.getError(productRsp));
					}
				}
			}
			else
			{
				_log.warning(TaobaoService.getError(itemGetRsp));
			}
		} catch (ApiException e) {
			_log.log(Level.SEVERE, "", e);
		}
		return SUCCESS;
	}

	public void setNumIid(long numIid) {
		this.numIid = numIid;
	}

	public Item getItem() {
		return item;
	}
	
	public String[] getPropKeywords()
	{
		String propsName = item.getPropsName();
		_log.info("propsName: " + propsName);
		if (propsName != null)
		{
			String[] props = propsName.split(";");
			String[] keywords = new String[props.length];
			for (int i = 0, n = props.length; i < n; i++)
			{
				String prop = props[i];
				if (!StringUtils.isEmpty(prop))
				{
					keywords[i] = prop.split(":")[3];
				}
			}
			return keywords;
		}
		return null;
	}

	public void setLeafCategoryWords(String[] leafCategoryWords) {
		this.leafCategoryWords = leafCategoryWords;
	}

	public String[] getLeafCategoryWords() {
		return leafCategoryWords;
	}
	
	public String getDefaultCoreWord()
	{
		if (product != null)
		{
			return product;
		}
		return leafCategoryWords[0];
	}

	public void setProduct(String product) {
		this.product = product;
	}

	public String getProduct() {
		return product;
	}
}
